import consola from 'consola'
import { defineCachedStore } from 'pinia-cached-store'
const BASE_URL = import.meta.env.VITE_BASE_URL

export const useDataStore = defineCachedStore({
  id: 'data',

  state: () => ({
    appVersion: import.meta.env.VITE_APP_VERSION as string | undefined,
    customers: [],
    customersSmall: [],
    customersMedium: [],
    customersLarge: [],
    customersXlarge: [],
    products: [],
    productsSmall: [],
    productsOrdersSmall: [],
    countryData: [],
    treetablenodesData: [],
    treenodesData: [],
    imagesData: []
  }),

  async refresh({ name }: { name: string }) {
    consola.debug(`${name} fetching data ...`)
    await fetch(`${BASE_URL}data/customers-medium.json`)
      .then(res => res.json())
      .then((d) => {
        this.customers = d.data
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/products.json`)
      .then(res => res.json())
      .then((d) => {
        this.products = d.data
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/products-small.json`)
      .then(res => res.json())
      .then((d) => {
        this.productsSmall = d.data
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/products-orders-small.json`)
    .then(res => res.json())
    .then((d) => {
      this.productsOrdersSmall = d.data
    })
    .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/countries.json`)
      .then(res => res.json())
      .then((d) => {
        this.countryData = d.data
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/treetablenodes.json`)
      .then(res => res.json())
      .then((d) => {
        this.treetablenodesData = d.root
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/treenodes.json`)
      .then(res => res.json())
      .then((d) => {
        this.treenodesData = d.root
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/photos.json`)
      .then(res => res.json())
      .then((d) => {
        this.imagesData = d.data
      })
      .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/customers-small.json`)
    .then(res => res.json())
    .then((d) => {
      this.customersSmall = d.data
    })
    .catch(error => consola.error(error))
    
    await fetch(`${BASE_URL}data/customers-medium.json`)
    .then(res => res.json())
    .then((d) => {
      this.customersMedium = d.data
    })
    .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/customers-large.json`)
    .then(res => res.json())
    .then((d) => {
      this.customersLarge = d.data
    })
    .catch(error => consola.error(error))

    await fetch(`${BASE_URL}data/customers-xlarge.json`)
    .then(res => res.json())
    .then((d) => {
      this.customersXlarge = d.data
    })
    .catch(error => consola.error(error))
  },

  caching: {
    // force reload on new version
    checkValidity: state =>
      (state.appVersion ?? '') === import.meta.env.VITE_APP_VERSION,
  },
})
